package com.breakfast.fun.adapter.user;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.breakfast.fun.R;
import com.breakfast.fun.base.BaseListAdapter;
import com.breakfast.fun.data.entity.user.address.AddressData;
import com.breakfast.fun.event.FinishEvent;
import com.breakfast.fun.ui.user.address.EditAddressActivity;
import com.breakfast.fun.util.L;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2016/9/9.
 */
public class UserAddressAdapter extends BaseListAdapter<AddressData> {
    private Boolean isFromCart = false;
    private Boolean isNeedShowChecked = true;
    private static AddressData curAddress;

    public UserAddressAdapter(Context context, List<AddressData> list, Boolean isFromCart) {
        super(context, list);
        this.isFromCart = isFromCart;
    }

    @Override
    public View getView(int i, View convertView, ViewGroup viewGroup) {
        final ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_user_address, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        AddressData data = getItem(i);
        L.d("addressadapter---" + data.getId() + ",addname---" + data.getAddress());
        holder.tvItemAddressAddress.setText(data.getAddress());
        holder.tvItemAddressMobile.setText(data.getPhone());
        holder.tvItemAddressName.setText(data.getConsignee());

        holder.llItemUserAddress.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isFromCart) {
                    EventBus.getDefault().post(new FinishEvent("UserAddressActivity"));
                    EventBus.getDefault().post(data);
                    return;
                }
                Intent intent = new Intent(mContext, EditAddressActivity.class);
                intent.putExtra("address", data);
                mContext.startActivity(intent);
                holder.mIvUserAddressCheck.setImageResource(R.drawable.ic_pay_checked);
                curAddress = data;
            }
        });

        holder.mIvUserAddressCheck.setTag(data.getId());
        //从购物车进入需要显示checked图标
        if (isFromCart) {
            holder.mIvUserAddressCheck.setVisibility(View.VISIBLE);
            if (curAddress != null) {
                if (holder.mIvUserAddressCheck.getTag().equals(getCurAddress().getId())) {
                    holder.mIvUserAddressCheck.setImageResource(R.drawable.ic_pay_checked);
                } else {
                    holder.mIvUserAddressCheck.setImageResource(R.drawable.ic_pay_unchecked);
                }
            } else {
                holder.mIvUserAddressCheck.setImageResource(R.drawable.ic_pay_unchecked);
            }
        } else {
            holder.mIvUserAddressCheck.setVisibility(View.GONE);
        }

        return convertView;

    }

    public Boolean getNeedShowChecked() {
        return isNeedShowChecked;
    }

    public void setNeedShowChecked(Boolean needShowChecked) {
        isNeedShowChecked = needShowChecked;
    }

    public AddressData getCurAddress() {
        return curAddress;
    }

    public void setCurAddress(AddressData curAddress) {
        this.curAddress = curAddress;
    }

    static class ViewHolder {
        @Bind(R.id.tv_item_address_name)
        TextView tvItemAddressName;
        @Bind(R.id.tv_item_address_mobile)
        TextView tvItemAddressMobile;
        @Bind(R.id.tv_item_address_address)
        TextView tvItemAddressAddress;
        @Bind(R.id.ll_item_user_address)
        LinearLayout llItemUserAddress;
        @Bind(R.id.iv_user_address_check)
        ImageView mIvUserAddressCheck;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
